package net.caixiaomi.info.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.CommonTab;

import java.util.List;

/**
 * 首页适配
 */

public class CommonFragmentPageAdapter extends FragmentStatePagerAdapter {

    //    private String mTitles[];
    private List<Fragment> mData;
    private List<CommonTab> mTitles;
    private String mFilter = "";

    public CommonFragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    public CommonFragmentPageAdapter(FragmentManager fm, List titles, List data) {
        super(fm);
        this.mTitles = titles;
        this.mData = data;
    }

    public int getCount() {
        return mTitles.size();
    }

    public Fragment getItem(int position) {
        Fragment fg = mData.get(position);
        CommonTab commonTab = mTitles.get(position);
        ((ComonFragmentImpl) fg).setType(commonTab.getType());
        ((ComonFragmentImpl) fg).setFilter(mFilter);
        return fg;
    }

    public CharSequence getPageTitle(int position) {
        return mTitles.get(position).getName();
    }

    public void clean() {
        try {
            for (Fragment fragment : mData) {
                fragment = null;
            }
            mData.clear();
            mTitles.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setFilter(String filter) {
        mFilter = filter;
    }

//    public View getTabView(int position) {
//        View v = LayoutInflater.from(getActivity()).inflate(R.layout.item_tab, null);
//        TextView text = (TextView) v.findViewById(R.id.text);
//        text.setText(getString(TITLES[position]));
//        return v;
//    }

}
